package com.aotain.oframework.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Get meta info frim /META-INF/MENIFEST.MF
 * 
 * @author Jacob.cw.Zhang@gmail.com
 */
public class Environment {
	private static final Logger logger = LoggerFactory.getLogger(Environment.class);
	private static final Properties props = new Properties();

	static {
		InputStream is = null;
		try {
			String path = getConfigPath() + "/version.txt";
			is = new FileInputStream(path);
			props.load(is);
		} catch (IOException e) {
			logger.error("Can not load {}.", getConfigPath() + "/version.txt", e);
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					logger.error("", e);
				}
			}
		}

	}

	public static String getConfigPath() {
		return System.getProperty("project.config.path");
	}

	public static String getAttribute(String attr) {
		return props.getProperty(attr, "");
	}

	public static String getProductStage() {
		return getAttribute("Product-Stage");
	}

	public static String getEnvironment() {
		return StringUtils.upperCase(System.getProperty("project.environment", ""));
	}

	public static String getCSSS2Tag() {
		return getAttribute("TAG");
	}

	public static String getBuildTag() {
		return getAttribute("BUILD_TAG");
	}

	public static String getProductVersion() {
		return getAttribute("RELEASE_REF_NO");
	}

	public static String getBuildDate() {
		return getAttribute("timestamp");
	}
}
